﻿#include "Widget.h"
#include "ui_widget.h"

#include <QGridLayout>
#include <QDomDocument>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 连接信号槽
    m_pHttp = new QtSoapHttpTransport(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(qqCheckOnline()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(getMobileCodeInfo()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(toTraditionalChinese()));
    connect(m_pHttp, SIGNAL(responseReady(const QtSoapMessage &)), this, SLOT(onResponse(const QtSoapMessage &)));
}

Widget::~Widget()
{
    delete ui;
}

// 获取QQ在线状态
void Widget::qqCheckOnline()
{
    QtSoapMessage message;

    // 设置方法
    message.setMethod("qqCheckOnline", "http://WebXml.com.cn/");

    // 设置动作
    m_pHttp->setAction("http://WebXml.com.cn/qqCheckOnline");

    // 设置主机
    m_pHttp->setHost("www.webxml.com.cn");

    // 添加方法参数
    QString strQQ = ui->lineEdit->text();
    message.addMethodArgument("qqCode", "", strQQ);
//    QString strXML = message.toXmlString();
//    qDebug() << strXML;

    // 提交请求
    m_pHttp->submitRequest(message, "/webservices/qqOnlineWebService.asmx");
}

// 获得国内手机号码归属地省份、地区和手机卡类型信息
void Widget::getMobileCodeInfo()
{
    QtSoapMessage message;

    // 设置方法
    message.setMethod("getMobileCodeInfo", "http://WebXml.com.cn/");

    // 设置动作
    m_pHttp->setAction("http://WebXml.com.cn/getMobileCodeInfo");

    // 设置主机
    m_pHttp->setHost("www.webxml.com.cn");

    // 添加方法参数
    QString strPhoneNumber = ui->lineEdit_3->text();
    message.addMethodArgument("mobileCode", "", strPhoneNumber);
//    QString strXML = message.toXmlString();
//    qDebug() << strXML;

    // 提交请求
    m_pHttp->submitRequest(message, "/WebServices/MobileCodeWS.asmx");
}

// 简体字转繁体字
void Widget::toTraditionalChinese()
{
    QtSoapMessage message;

    // 设置方法
    message.setMethod("toTraditionalChinese", "http://webxml.com.cn/");

    // 设置动作
    m_pHttp->setAction("http://webxml.com.cn/toTraditionalChinese");

    // 设置主机
    m_pHttp->setHost("www.webxml.com.cn");

    // 添加方法参数
    QString strSimplified = ui->lineEdit_5->text();
    message.addMethodArgument("sText", "", strSimplified);
//    QString strXML = message.toXmlString();
//    qDebug() << strXML;

    // 提交请求
    m_pHttp->submitRequest(message, "/WebServices/TraditionalSimplifiedWebService.asmx");
}

void Widget::onResponse(const QtSoapMessage &response)
{
    QString strXML = response.toXmlString();
    qDebug() << strXML;
    QDomDocument doc;
    doc.setContent(strXML);

    QDomNodeList nodeList;
    // 处理接收到的信息
    nodeList = doc.elementsByTagName("qqCheckOnlineResult");
    if (!nodeList.isEmpty())
    {
        handleQQCheckOnline(nodeList);
        return;
    }

    nodeList = doc.elementsByTagName("getMobileCodeInfoResult");
    if (!nodeList.isEmpty())
    {
        handleGetMobileCodeInfo(nodeList);
        return;
    }

    nodeList = doc.elementsByTagName("toTraditionalChineseResult");
    if (!nodeList.isEmpty())
    {
        handleToTraditionalChinese(nodeList);
        return;
    }
}

// 处理[获取QQ在线状态]回复
void Widget::handleQQCheckOnline(QDomNodeList nodeList)
{
    QDomNode node = nodeList.at(0);
    QString strResult = node.toElement().text();
    QString strState("N/A");
    if (QString::compare(strResult, "Y") ==0)
    {
        strState = QString::fromLocal8Bit("在线");
    }
    else if (QString::compare(strResult, "N") == 0)
    {
        strState = QString::fromLocal8Bit("离线");
    }
    else if (QString::compare(strResult, "E") == 0)
    {
        strState = QString::fromLocal8Bit("QQ号码错误");
    }
    else if (QString::compare(strResult, "A") == 0)
    {
        strState = QString::fromLocal8Bit("商业用户验证失败");
    }
    else if (QString::compare(strResult, "V") == 0)
    {
        strState = QString::fromLocal8Bit("免费用户超过数量");
    }
    ui->lineEdit_2->setText(strState);
}

// 处理[获取国内手机号码归属地省份、地区和手机卡类型信息]回复
void Widget::handleGetMobileCodeInfo(QDomNodeList nodeList)
{
    QDomNode node = nodeList.at(0);
    QString strResult = node.toElement().text();
    ui->lineEdit_4->setText(strResult);
}

// 处理[简体字转繁体字]回复
void Widget::handleToTraditionalChinese(QDomNodeList nodeList)
{
    QDomNode node = nodeList.at(0);
    QString strResult = node.toElement().text();
    ui->lineEdit_6->setText(strResult);
}
